package hjj.servlet;

import hjj.service.DeptDutyManager;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Dept;
import model.dept_duty_emp;
import util.Page;

public class DeptDutyManagerServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		int pageNo = 1;
		int pageSize = 10;
		DeptDutyManager ddm=new DeptDutyManager();
		String ddqueryinfo = "";
		String ddquerytype = "";
		Page<dept_duty_emp> page = null;
		try{
			if (request.getParameter("pageNo") != null) {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			}
			if (request.getParameter("pageSize") != null) {
				pageSize = Integer.parseInt(request.getParameter("pageSize"));

				request.getSession().setAttribute("pageSize", pageSize);
			}
			if (request.getSession().getAttribute("pageSize") != null) {
				pageSize = (Integer) request.getSession().getAttribute(
						"pageSize");
			}
			if (null != request.getParameter("ddquerytype")) {
				pageNo = 1;
				request.getSession().setAttribute("ddquerytype",
						request.getParameter("ddquerytype"));
			}
			if (null != request.getParameter("ddqueryinfo")) {
				request.getSession().setAttribute("ddqueryinfo",
						request.getParameter("ddqueryinfo"));
			}
			if (null != request.getSession().getAttribute("ddquerytype")) {
				ddquerytype = (String) request.getSession().getAttribute(
						"ddquerytype");
			}
			if (null != request.getSession().getAttribute("ddqueryinfo")) {
				ddqueryinfo = (String) request.getSession().getAttribute(
						"ddqueryinfo");
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		if (!"".equals(ddqueryinfo)&&!"ddall".equals(ddquerytype)) {
			if ("deptname".equals(ddquerytype)) {
				page = ddm.getByDept(pageNo, pageSize, ddqueryinfo);
			}
			if ("dutyname".equals(ddquerytype)) {
				page = ddm.getByDuty(pageNo, pageSize, ddqueryinfo);
			}
		} else {
			page = ddm.getAll(pageNo, pageSize);
		}
		request.setAttribute("page", page);
		request.getRequestDispatcher("System/DeptDutyManager.jsp").forward(request,
				response);
	}
}
